<?php
/**
 * Created by liucunzhou for jiyichi's system
 */
namespace app\common\customer\validate;

use app\common\customer\model\CustomerContact;
use think\Validate;

class CustomerAppendValidate extends Validate
{
    protected $rule = [
        'mobile|wechat|telephone' => 'requireAtLeastOne',
        'mobile' => 'checkContact',
        'wechat' => 'checkContact',
        'telephone' => 'checkContact',
        'zone' => ['require'],
        'source_id' => ['require', 'gt:0'],
        'remark' => ['require']
    ];

    protected function requireAtLeastOne($value, $rule, $data)
    {
        return !empty($data['mobile']) || !empty($data['wechat']) || !empty($data['telephone']) ? true : 'At least one contact method must be provided';
    }

    protected function checkContact($value, $rule, $data)
    {
        if (!empty($value)) {
            $model = new CustomerContact();
            $count = $model->where('contact', $value)->count();
            return $count > 0 ? "{$value}已存在": true;
        }
        return true;
    }

    protected $message = [
        'zone.require' => '请选择区域',
        'source_id.require' => '请选择来源',
        'remark.require' => '请录入备注'
    ];
}